//
//  HomeScreen.swift
//  TanTan
//
//  Created by 于航 on 2024/9/15.
//

import SwiftUI

struct HomeScreen: View {
    @EnvironmentObject var appState : AppState
    @StateObject var homeViewModel = HomeViewModel()
    var body: some View {
        VStack(spacing : 0) {
            if !appState.isFullScreen {
                HStack {
                    HomeTitleButton(selectTitle: $homeViewModel.selectedTitle, title: .MIAOTAN)
                    HomeTitleButton(selectTitle: $homeViewModel.selectedTitle, title: .PICKS)
                    Spacer()
                }
                .padding([.leading, .top], 15)
            }
            switchTab()
        }
    }
    
    @ViewBuilder
    func switchTab() ->some View {
        switch homeViewModel.selectedTitle {
        case .MIAOTAN:
            if homeViewModel.hasMoreCard {
                ZStack {
                    ForEach(homeViewModel.displayingCard.reversed()) { card in
                        CardContainsView(viewModel: homeViewModel, card: card)
                            .environmentObject(appState)
                    }
                }
            } else {
                NoResultView()
            }
        case .PICKS:
            PickListView(cards: homeViewModel.displayingCard)
        }
    }
    
    enum HomeTitle : String {
        case MIAOTAN
        case PICKS
    }
}

struct HomeTitleButton : View {
    @Binding var selectTitle : HomeScreen.HomeTitle
    var title : HomeScreen.HomeTitle
    var body: some View {
        Button {
            selectTitle = title
        } label: {
            Text(title.rawValue)
                .font(.system(size: 25))
                .fontWeight(.bold)
                .foregroundColor(selectTitle == title ? Color.accentColor : .gray.opacity(0.3))
        }

    }
}

#Preview {
    HomeScreen()
        .environmentObject(AppState())
}
